//
//  WBStatusPictureView.swift
//  daoge_sineweibo
//
//  Created by 伍禄林 on 16/10/16.
//  Copyright © 2016年 伍禄林. All rights reserved.
//

import UIKit

class WBStatusPictureView: UIView {
    
    var viewModel: WBStatusViewModel?{
        
        didSet{
            caclViewSize()
            urls = viewModel?.picUrls
        }
    }
    
    func caclViewSize() {
    
        if viewModel?.picUrls?.count == 1 {//单图
            
            let viewSize = viewModel?.pictureSize ?? CGSize()
            
            let v = subviews[0]
            
            v.frame = CGRect(x: 0, y: WBStatusPictureViewOutMargin, width: viewSize.width, height: viewSize.height - WBStatusPictureViewOutMargin)
            
        }else {//多图
            
            let v = subviews[0]
            
            v.frame = CGRect(x: 0, y: WBStatusPictureViewOutMargin, width: WBStatiusPitureItemWidth, height: WBStatiusPitureItemWidth)
            
        }
        heightCons.constant = viewModel?.pictureSize.height ?? 0
    }
    
    private var urls: [WBPictureModel]? {
        
        didSet {
            
            for view in subviews {
                view.isHidden = true
            }
            
            guard let urls = urls else {
                return
            }
            
            var index = 0
            
            for model in urls {
                
                let iv = subviews[index] as! UIImageView
                
                if index == 1 && urls.count == 4 {
                    index += 1
                }
                
                iv.cz_setImage(urlString: model.thumbnail_pic, placeholderImage: nil)
                
                iv.isHidden = false
                
                index += 1
                
            }
            
//            for var i in 0..<urls.count {
//                
//                let iv = subviews[i] as! UIImageView
//                
//                if i == 1 && urls.count == 4 {
//                    
//                    i += 1
//                    
//                }
//                iv.cz_setImage(urlString: (urls[i] as? WBPictureModel)?.thumbnail_pic, placeholderImage: nil)
//                
//                iv.isHidden = false
//            }
            
        }
        
    }

    @IBOutlet weak var heightCons: NSLayoutConstraint!

    
    override func awakeFromNib() {
        setupUI()
    }
}

extension WBStatusPictureView {
    
    func setupUI()  {
        
        clipsToBounds = true
        
        let count = 3
        
        let rect = CGRect(x: 0, y: WBStatusPictureViewOutMargin, width: WBStatiusPitureItemWidth, height: WBStatiusPitureItemWidth)
        
        for i in 0..<count * count {
            
            let vi = UIImageView()
            
            vi.contentMode = .scaleAspectFill
            vi.clipsToBounds = true
            
            //行
            let row = CGFloat(i / count)
            //列
            let col = CGFloat(i % count)
            
            let xOffest = col * (WBStatiusPitureItemWidth + WBStatusPictureViewInMargin)
            
            let yOffect = row * (WBStatiusPitureItemWidth + WBStatusPictureViewInMargin)
            
            vi.frame = rect.offsetBy(dx: xOffest, dy: yOffect)
            
            addSubview(vi)
        }
        
    }
    
}
